const Promise = require('./es6-promise.js')
const db = wx.cloud.database();
const qsbdcHost = "http://word.qsbdc.com/wl.php?tag=all";
const audioHost = "http://sound.yywz123.com/qsbdcword/";

var level = 10;
var pageBegin = 36;
var pageEnd = 40;
//var justFilter = ",incertitude,gargoyle,fanfare,";
var justFilter = "";

const reptileQuery = () => {
  return;
  for (var page = pageBegin; page <= pageEnd; page++) {
    sleep(1000);
    //http://phrase.qsbdc.com/wl.php?level=1&&tag=all&&page_id=2
    let url = 'http://phrase.qsbdc.com/wl.php?tag=all&level=' + level + '&page_id=' + page;
    //console.info(url);
    wx.request({
      url: url,
      success(res) {
        var html = res.data;
        var arr = html.match(/<table([\s\S]*?)class=\"table_solid\">([\s\S]*?)<\/table>/g);

        if (arr && arr.length > 0) {
          parseHtml(arr[0], level);
        }
      }
    })
  }

  //console.info("==============");
  //console.info(wordDb);

}

function parseHtml(html, level) {
  //<span class="hidden_1_1" style="cursor:pointer;">bag</span>
  //<span class="hidden_2_1" style="cursor:pointer;">[bæg]</span>
  //<span class="hidden_3_1" style="cursor:pointer;">n.袋，包，钱包，背包</span>
  // <a class="mytitle" href="#"
  //   title="单词 many 的例句||||There are many books. 那里有很多书。||||How many machines in your factory? 你的工厂有多少太台机器？">例句</a>

  var wordArr = html.match(/<span class=\"hidden_1_1\"([\s\S]*?)<\/span>/g);
  //var symbolArr = html.match(/<span class=\"hidden_2_1\"([\s\S]*?)<\/span>/g);
  var explainlArr = html.match(/<span class=\"hidden_3_1\"([\s\S]*?)<\/span>/g);
  var examplelArr = html.match(/title=\"词组([\s\S]*?)\">/g);
  var namelArr = html.match(/title=\"听读音([\s\S]*?)href=\"###\"/g);

  if (wordArr==null)
  {
      console.info("没有抓取到单词");
      return;
  }
  //剔除html和空格
  wordArr = wordArr.map(function(item) {
    item = item.replace(/<\/?.+?>/g, "");
    return item;
  });

  //剔除html和空格
  // symbolArr = symbolArr.map(function(item) {
  //   item = item.replace(/<\/?.+?>/g, "");
  //   item = item.replace(/\s+/g, "");
  //   return item;
  // });

  //剔除html和空格
  explainlArr = explainlArr.map(function(item) {
    item = item.replace(/<\/?.+?>/g, "");
    item = item.replace(/\s+/g, "");
    return item;
  });

  //解析例句
  //"title="单词bag的例句||||Mybagisnotblackbutred.我的书包不是黑色的而是红色的。||||Pleaseopenthisbag.请打开这个袋子。">"
  examplelArr = examplelArr.map(function(item) {
    item = item.substring(0, item.length - 2);
    item = item.replace(/&#039;/g, "'");
    var list = item.split("||||");
    list = list.slice(1, list.length);
    return list;
  });


  //解析name
  //title="听读音"style="vertical-align:middle;"name="QzOzA2"href="###"
  namelArr = namelArr.map(function(item) {
    item = item.match(/name=\"([\s\S]*?)\"/g)[0];
    item = item.replace(/\"/g, "");
    item = item.replace(/name=/g, "");


    return item;
  });

  //return;

  for (var i = 0; i < wordArr.length; i++) {
    let newWord = null;
    try{
      newWord = {
        "level": level,
        "phrase": wordArr[i],
        "explain": explainlArr[i],
        "example": examplelArr[i],
        "audio": audioHost + namelArr[i].substring(0, 1) + "/" + namelArr[i] + ".mp3",
        "name": namelArr[i]
      }
    }catch(e)
    {
      continue;
    }
 
    //continue;
    if ("" != justFilter)
    {
      if (justFilter.indexOf("," + newWord.word+",")<0)
     {
       continue;
     }
      
    }
    savePhrase(newWord);

    //

  }



}

var wordDb="";

function savePhrase(newWord) {
  
    //console.info("保存======");
   
  //wordDb += JSON.stringify(newWord);


    //保存
  db.collection("phrases").add({
      data: newWord,
      success: res => {
        console.info("保存成功======");
      },
      fail: e => {
        
        //console.error(e.message);

        if (e.message.indexOf("dup key")>0)
        {
          console.error("重复保存======");
        }else{
          console.error("保存失败======" + newWord.word);
        }
        
        
      }
    })

}


function sleep(delay) {
  var start = (new Date()).getTime();
  while ((new Date()).getTime() - start < delay) {
    continue;
  }
}

//剔除html
function removeHtml(str) {
  str = str.replace(/<\/?.+?>/g, "");
  return str;
}
//剔除空格
function removeBlank(str) {
  str = str.replace(/\s+/g, "");
  return str;
}

module.exports = {
  reptileQuery: reptileQuery
}